import { useRef } from "react";
import Loading, { type RefMenthodType } from "@/components/Loading";

/** 页面loading, 使用方式: 
 * const { Loading, showLoading, hideLoading } = useLoading();
 * 页面中使用 {Loading}
 * showLoading 显示loading
 * hideLoading 隐藏loading
 */

export default () => {
  const loadingRef = useRef<RefMenthodType>();

  const showLoading = (msg?: string) => {
    if (loadingRef.current) {
      loadingRef.current.show(msg);
    }
  };

  const hideLoading = () => {
    if (loadingRef.current) {
      loadingRef.current.hide();
    }
  };

  return {
    Loading: <Loading ref={loadingRef} />,
    showLoading,
    hideLoading,
    ref: loadingRef,
  }
};
